package com.yf.dxs.wo.service;

import com.alibaba.fastjson2.JSON;
import com.yf.dxs.wo.consts.WoTopicConst;
import com.yf.dxs.wo.rep.WoException;
import com.yf.dxs.wo.req.entity.WoEvent;
import com.yf.dxs.wo.service.impl.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class EventHandler {

    @Resource
    private OrderHandler orderHandler;

    @Resource
    private UserHandler userHandler;

    @Resource
    private MemberCardHandler memberCardHandler;

    @Resource
    private MemberCardLevelHandler memberCardLevelHandler;

    @Resource
    private GoodsHandler goodsHandler;

    public void handle(String msg) {
        try {
            WoEvent woEvent = JSON.parseObject(msg, WoEvent.class);
            log.info("收到信息:{},{},{}", woEvent.getTopic(), woEvent.getEvent(),woEvent.getMsgBody());
            if (woEvent.getTopic().equals(WoTopicConst.ORDER)) { //交给Order处理
                orderHandler.handle(woEvent.getId(), woEvent.getMsgBody(), woEvent.getEvent());
            }
            if (woEvent.getTopic().equals(WoTopicConst.BOS_USER)) {
                userHandler.handle(woEvent.getId(), woEvent.getMsgBody(), woEvent.getEvent());
            }
            if (woEvent.getTopic().equals(WoTopicConst.MEMBER_CARD)) {
                memberCardHandler.handle(woEvent.getId(), woEvent.getMsgBody(), woEvent.getEvent());
            }
            if (woEvent.getTopic().equals(WoTopicConst.MEMBER_CARD_LEVEL)) {
                memberCardLevelHandler.handle(woEvent.getId(), woEvent.getMsgBody(), woEvent.getEvent());
            }
            if(woEvent.getTopic().equals(WoTopicConst.SHOP_GOODS)){
                goodsHandler.handle(woEvent.getId(),woEvent.getMsgBody(),woEvent.getEvent());
            }
        } catch (Exception e) {
            log.error("处理订阅失败:{}", msg, e);
            throw new WoException(e);
        }
    }

}
